package com.ly.algorithm.offerpointer;

/**
 * @Classname Offer25
 * @Description
 *
 * 输入两个递增排序的链表，合并这两个链表并使新链表中的节点仍然是递增排序的。
 *
 * 示例1：
 *
 * 输入：1->2->4, 1->3->4
 * 输出：1->1->2->3->4->4
 *
 * @Date 2021/1/27 17:09
 * @Author 冷心影翼
 */
public class Offer25 {

    public static void main(String[] args) {
        Solution25 solution25 = new Solution25();
        solution25.mergeTwoLists(ListNode.init(new int[]{1,2,4}),ListNode.init(new int[]{2,3,4}))
            .printRes();
    }

}


class Solution25 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode pre = head;
        while (l1!=null && l2!=null) {
            if(l1.val<=l2.val) {
                pre.next = l1;
                l1 = l1.next;
            }else {
                pre.next = l2;
                l2 = l2.next;
            }
            pre = pre.next;
        }
        pre.next = l1==null?l2:l1;
        return head.next;
    }
}